package com.mia.props.common.entities;

import com.mia.craftstudio.CSModel;
import com.mia.craftstudio.libgdx.Vector3;
import com.mia.craftstudio.minecraft.CraftStudioModelWrapper;
import com.mia.props.client.fx.ParticleHandler;
import com.mia.props.common.TileProps;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import net.minecraft.world.World;

public class TileParticleEmitter extends TileProps {
   List emittersOnRender;
   List emittersOnUpdate;

   public void renderParticles(World world, int x, int y, int z, Random random) {
      if (this.emittersOnRender == null) {
         this.generateEmitters();
      }

      this.renderParts(world, this.emittersOnRender);
   }

   public void updateEntity() {
      if (this.worldObj.isRemote) {
         if (this.emittersOnUpdate == null) {
            this.generateEmitters();
         }

         this.renderParts(this.worldObj, this.emittersOnUpdate);
      }

   }

   public boolean canUpdate() {
      return true;
   }

   protected void renderParts(World world, List emitters) {
      Iterator var3 = emitters.iterator();

      while(true) {
         while(var3.hasNext()) {
            TileParticleEmitter.ParticleEmitter pe = (TileParticleEmitter.ParticleEmitter)var3.next();
            if (pe.speed == 1) {
               ParticleHandler.INSTANCE.renderParticle(pe.type, world, pe.x, pe.y, pe.z, 0.0D, 0.0D, 0.0D, pe.size);
            } else {
               for(int i = 0; i < this.rand.nextInt(pe.speed) + pe.speed / 2; ++i) {
                  ParticleHandler.INSTANCE.renderParticle(pe.type, world, pe.x, pe.y, pe.z, 0.0D, 0.0D, 0.0D, pe.size);
               }
            }
         }

         return;
      }
   }

   protected void generateEmitters() {
      this.emittersOnUpdate = new ArrayList();
      this.emittersOnRender = new ArrayList();
      CraftStudioModelWrapper.NodeHashCache nodeCache = this.getModelData().wrapper.nodeCache;
      Collection nodes = this.getModelData().csmodel.getNodes();
      Iterator var3 = nodes.iterator();

      while(var3.hasNext()) {
         CSModel.ModelNode node = (CSModel.ModelNode)var3.next();
         if (node.getName().startsWith("ParticleEmitter_")) {
            String key = node.getName().replace("ParticleEmitter_", "").toLowerCase() + "_";
            String type = "magic";
            float size = 1.0F;
            int speed = 1;
            boolean onupdate = false;
            if (this.tileParams.containsKey(key + "type")) {
               type = (String)this.tileParams.get(key + "type");
            }

            if (this.tileParams.containsKey(key + "size")) {
               size = Float.valueOf((String)this.tileParams.get(key + "size"));
            }

            if (this.tileParams.containsKey(key + "speed")) {
               speed = Integer.valueOf((String)this.tileParams.get(key + "speed"));
            }

            if (this.tileParams.containsKey(key + "onUpdate")) {
               onupdate = Boolean.valueOf((String)this.tileParams.get(key + "onUpdate"));
            }

            CraftStudioModelWrapper.NodeWrapper nodeWrapper = nodeCache.get(node);
            if (onupdate) {
               this.emittersOnUpdate.add(new TileParticleEmitter.ParticleEmitter(type, size, speed, onupdate, this.xCoord, this.yCoord, this.zCoord, nodeWrapper.getExtend(this.rotation)[0]));
            } else {
               this.emittersOnRender.add(new TileParticleEmitter.ParticleEmitter(type, size, speed, onupdate, this.xCoord, this.yCoord, this.zCoord, nodeWrapper.getExtend(this.rotation)[0]));
            }
         }
      }

   }

   class ParticleEmitter {
      public final int speed;
      public final String type;
      public final float size;
      public final Vector3 pos;
      public final double x;
      public final double y;
      public final double z;
      public final boolean onUpdate;

      public ParticleEmitter(String type, float size, int speed, boolean onUpdate, int x, int y, int z, Vector3 pos) {
         this.type = type;
         this.speed = speed;
         this.size = size;
         this.onUpdate = onUpdate;
         this.pos = pos;
         this.x = (double)(this.pos.x + (float)x);
         this.y = (double)(this.pos.y + (float)y);
         this.z = (double)(this.pos.z + (float)z);
      }

      public String toString() {
         return String.format("[Particle Emitter : %s %s %s]", this.type, this.size, this.pos);
      }
   }
}
